#Twisted Shootout
#Copyright (C) 2007  Han Dao
#
#This program is free software: you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation, either version 3 of the License, or
#(at your option) any later version.
#
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#GNU General Public License for more details.
#
#You should have received a copy of the GNU General Public License
#along with this program.  If not, see <http://www.gnu.org/licenses/>.

#You can contact the author at wikipediankiba@gmail.com
import main
import pygame
import data
import sys
import os
import mapengine
import player
import weapontrack
import enemytrack
import collide
import item
import statusbar

class GameMode:
	def __init__(self,name):
		self.clname, self.cloud = data.read.load('background')
		self.sprites = pygame.sprite.RenderPlain()
		self.w = weapontrack.WeaponTracker(self.sprites)
		self.e = enemytrack.EnemyTracker(self.sprites,self.w)
		self.p = player.Player(self.sprites,self.w)
		self.collide = collide.Collision_Detector(self.w,self.e,self.p,self.sprites)
		self.mapsprites = pygame.sprite.RenderPlain()
		self.engine = mapengine.MapEngine(self.mapsprites,name)
		self.engine.setup(self.p,self.e,self.w)
		self.engine.position.loadcollide(self.collide)
		self.statusprites = pygame.sprite.RenderPlain()
		self.health = statusbar.StatusBar(self.p,(0,0,50,50),fullAmt= 30)
		self.ammo = statusbar.StatusBar(self.w,(750,0,50,50), attrName = "ammo") 
		self.statusprites.add((self.health,self.ammo))
		self.p.changelocation(self.engine.obj[1])
	def run(self):
		main.display.blit(self.cloud,(0,0))
		while 1:
			if self.p.win == False:
				self.p.retainer = [self.p.rect.x,self.p.rect.y]
				for e in self.e.e:
					e.retainer = [e.rect.x,e.rect.y]
				for e in pygame.event.get():
					if e.type == pygame.QUIT:
						sys.exit()
					if e.type == pygame.KEYDOWN:
						if e.key == pygame.K_ESCAPE:
							sys.exit()
						elif e.key == pygame.K_RIGHT:
							self.p.right()
						elif e.key == pygame.K_LEFT:
							self.p.left()
						elif e.key == pygame.K_UP:
							self.p.up()
						elif e.key == pygame.K_SPACE:
							self.p.activate()
						elif e.key == pygame.K_d:
							self.p.flipright()
						elif e.key == pygame.K_a:
							self.p.flipleft()
						elif e.key == pygame.K_f:
							self.p.knife()
					if e.type == pygame.KEYUP:
						if e.key == pygame.K_RIGHT:
							self.p.upright()
						elif e.key == pygame.K_LEFT:
							self.p.upleft()
						elif e.key == pygame.K_SPACE:
							self.p.deactivate()
				self.execute()	
				if self.p.health < 1:
					self.p.win = -1
				self.draw()
				pygame.display.flip()
			elif self.p.win == -1:
				image = data.read.load("dead",True)
				main.display.blit(image,(0,0))
				return
			elif self.p.win == True:
				image = data.read.load('winscreen',True)
				main.display.blit(image,(0,0))
				return
	def execute(self):
		self.collide.detect()
		self.collide.knifecombat()
		self.p.execute()
		self.w.move()
		self.e.execute()
		self.engine.position.compute()	
	def draw(self):
		main.display.blit(self.cloud,(0,0))
		self.mapsprites.draw(main.display)
		self.statusprites.update()
		self.statusprites.draw(main.display)
		self.engine.items.sprites.draw(main.display)
		self.sprites.draw(main.display)